|
1
|
|
|
const SecuritySchemaParser = require('./SecuritySchemesParser'), |
|
2
|
|
|
ParserInterface = require('../ParserInterface'), |
|
3
|
|
|
spawn = require('child_process'), |
|
4
|
|
|
_ = require('lodash'), |
|
5
|
|
|
MethodsParser = require('./MethodsParser') |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @ignore module.exports |
|
9
|
|
|
* @ignore exports |
|
10
|
|
|
* @ignore spawn |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @class Parser |
|
15
|
|
|
* Parser for swagger |
|
16
|
|
|
* |
|
17
|
|
|
* @property {Swagger20} data Swagger data |
|
18
|
|
|
* @property {String|undefined} packageVersion Package version. If define command for get tags list not will be running |
|
19
|
|
|
* @property {String} getTagCommand Command for get tag lists. If <code>packageVersion</code> is empty |
|
20
|
|
|
* auto detect tag list and get last tag for detect package version |
|
21
|
|
|
* @property {String} packageName Package name |
|
22
|
|
|
* @property {String} className Class name |
|
23
|
|
|
* @property {String} moduleName Module name |
|
24
|
|
|
* @property {SecuritySchemaParser} schemeParser Security scheme parser |
|
25
|
|
|
* @property {MethodsParser} methodsParser Methods parser |
|
26
|
|
|
* @property {String} modelPath Model path |
|
27
|
|
|
* @property {String} docsPath Methods path |
|
28
|
|
|
*/ |
|
29
|
|
|
class Parser extends ParserInterface { |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor |
|
33
|
|
|
* |
|
34
|
|
|
* @param {Swagger20} data Swagger api data |
|
35
|
|
|
* @param {ParserOptions} options Parser options |
|
36
|
|
|
*/ |
|
37
|
|
|
constructor (data, options) { |
|
38
|
|
|
super() |
|
39
|
|
|
|
|
40
|
|
|
options = options || {} |
|
41
|
|
|
|
|
42
|
|
|
if (data.swagger !== '2.0') { |
|
43
|
|
|
throw new Error('Unsupported swagger version by parser') |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
this.data = data |
|
47
|
|
|
this.packageVersion = options.packageVersion |
|
48
|
|
|
this.getTagCommand = options.getTagCommand || 'git tag -l' |
|
49
|
|
|
this.repoPath = options.repo || __dirname |
|
50
|
|
|
this.packageName = options.packageName || 'test-api' |
|
51
|
|
|
this.schemaParser = new SecuritySchemaParser(this.data.securityDefinitions) |
|
52
|
|
|
this.className = options.className |
|
53
|
|
|
this.moduleName = options.moduleName |
|
54
|
|
|
this.modelPath = options.modelPath |
|
55
|
|
|
this.docsPath = options.docsPath |
|
56
|
|
|
options.methodsParserConfig = options.methodsParserConfig || { |
|
57
|
|
|
parameterParserConfig: { |
|
58
|
|
|
addEnumDescription: true |
|
59
|
|
|
}, |
|
60
|
|
|
className : this.className, |
|
61
|
|
|
moduleName : this.moduleName, |
|
62
|
|
|
packageName : this.packageName, |
|
63
|
|
|
modelPath : this.modelPath, |
|
64
|
|
|
docsPath : this.docsPath |
|
65
|
|
|
} |
|
66
|
|
|
this.methodsParser = new MethodsParser(this.data.paths, this.schemaParser, data.definitions, options.methodsParserConfig) |
|
67
|
|
|
|
|
68
|
|
|
this._initData(options) |
|
69
|
|
|
this._prepareDefinitions() |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get package version |
|
74
|
|
|
* |
|
75
|
|
|
* @return {null|string} |
|
76
|
|
|
* @private |
|
77
|
|
|
*/ |
|
78
|
|
|
_getPackageVersion () { |
|
79
|
|
|
if (this.packageVersion) { |
|
80
|
|
|
return this.packageVersion |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
let gitTags = spawn.execSync('cd ' + this.repoPath + '; ' + this.getTagCommand).toString().split('\n').reverse() |
|
84
|
|
|
gitTags = gitTags.slice(1, gitTags.length) |
|
85
|
|
|
|
|
86
|
|
|
if (!gitTags.length) { |
|
87
|
|
|
gitTags.push('dev-master@dev') |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
this.packageVersion = gitTags[0] |
|
91
|
|
|
|
|
92
|
|
|
return this.packageVersion |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Init base swagger data |
|
97
|
|
|
* |
|
98
|
|
|
* @param {ParserOptions} options Parser options |
|
99
|
|
|
* @private |
|
100
|
|
|
*/ |
|
101
|
|
|
_initData (options) { |
|
102
|
|
|
this.swagger = {} |
|
103
|
|
|
this.swagger.title = this.data.title |
|
104
|
|
|
this.swagger.description = this.data.info.description |
|
105
|
|
|
this.swagger.apiVersion = this.data.info.version |
|
106
|
|
|
this.swagger.packageVersion = this._getPackageVersion() |
|
107
|
|
|
this.swagger.isSecure = typeof this.data.securityDefinitions !== 'undefined' |
|
108
|
|
|
this.swagger.className = options.className |
|
109
|
|
|
this.swagger.moduleName = options.moduleName |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Prepare definitions (project models) |
|
114
|
|
|
* |
|
115
|
|
|
* @private |
|
116
|
|
|
*/ |
|
117
|
|
|
_prepareDefinitions () { |
|
118
|
|
|
this.swagger.definitions = this.data.definitions |
|
119
|
|
|
this.swagger.definitionsGroup = [] |
|
120
|
|
|
|
|
121
|
|
|
let _self = this |
|
122
|
|
|
|
|
123
|
|
|
_.each(this.swagger.definitions, (definition, name) => { |
|
124
|
|
|
_self.swagger.definitions[name].enums = []; |
|
125
|
|
|
_self._addDefinitionToGroup(name, definition) |
|
126
|
|
|
if (definition.properties) { |
|
127
|
|
|
_.each(definition.properties, (prop, attrName) => { |
|
128
|
|
|
if (prop.enum) { |
|
129
|
|
|
let _enum = { |
|
130
|
|
|
name : _.upperFirst(_.camelCase(attrName)) + ' Enum', |
|
131
|
|
|
camelCaseName: _.camelCase(attrName), |
|
132
|
|
|
values : prop.enum, |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
_enum.description = prop.description |
|
136
|
|
|
|
|
137
|
|
|
_self.swagger.definitions[name].enums.push(_enum) |
|
138
|
|
|
} |
|
139
|
|
|
}) |
|
140
|
|
|
} |
|
141
|
|
|
}) |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Generate definitions group |
|
146
|
|
|
* |
|
147
|
|
|
* @param {String} group Group name |
|
148
|
|
|
* @param {Object} definition Swagger definition |
|
149
|
|
|
* @private |
|
150
|
|
|
*/ |
|
151
|
|
|
_addDefinitionToGroup (group, definition) { |
|
152
|
|
|
if (typeof definition !== 'object') { |
|
153
|
|
|
return |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if (typeof this.swagger.definitionsGroup[group] === 'undefined') { |
|
157
|
|
|
this.swagger.definitionsGroup[group] = [] |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
this.swagger.definitionsGroup[group].push(definition) |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Parse data and return prepared data for templates |
|
165
|
|
|
* |
|
166
|
|
|
* @return {Swagger20} |
|
167
|
|
|
*/ |
|
168
|
|
|
parse () { |
|
169
|
|
|
this.swagger.security = this.schemaParser.parse() |
|
170
|
|
|
this.swagger.methods = this.methodsParser.parse(this.swagger.definitions) |
|
171
|
|
|
this.swagger.tagsGroup = this.methodsParser.methodsGroup |
|
172
|
|
|
this.swagger.modelPath = this.modelPath |
|
173
|
|
|
this.swagger.docsPath = this.docsPath |
|
174
|
|
|
|
|
175
|
|
|
return this.swagger |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
module.exports = Parser |